// SPDX-License-Identifier: GPL-2.0
/*
 * drivers/iio/adc/lombo/csp/common.c - csp common operations
 *
 * Copyright (C) 2016-2018, LomboTech Co.Ltd.
 * Author: lomboswer <lomboswer@lombotech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "common.h"

#ifdef CONFIG_LOMBO_MISC
u64 csp_get_efuse_value(u32 module_id)
{
	int ret = 0;
	u32 module_len = 0;
	u8 buf[BUF_LEN];
	u64 value = 0;
	int i;

	/* get the length of module in efuse */
	ret = lombo_get_efuse_module_len(module_id, &module_len);
	if (ret != 0) {
		PRT_ERR("Can not get moudle len!, AT: %d\n", ret);
		return 0;
	}

	if (module_len < 0 || module_len > BUF_LEN)
		PRT_ERR("module_len:%d\n", module_len);
	PRT_DBG("module_len:%d\n", module_len);

	/* read the module in efuse and save it in buf */
	ret = lombo_efuse_read(module_id, (void *)buf, module_len);
	if (ret != 0) {
		PRT_ERR("Can not read efuse!\n");
		return 0;
	}

	/* value: the value of a bit in a register */
	for (i = 0; i < module_len; i++)
		value = value | ((*(buf + i)) << (i * 8));

	return value;
}
#else
u64 csp_get_efuse_value(u32 module_id)
{
	return 0;
}
#endif
